var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    addresslist: [],

    del_address_id: 0,   //存储删除收货地址id

    check_address_id: 0, //选择的ID
    check_flag: false,    //选则框是否展示  来自会员中心->收货地址 则不展示  来自提交订单->选择收货地址 则显示

    from_page: '' //来源页面  checkout(需设置checkout页面的back_address_id)、会员中心(不需操作)
  },

  //选择地址
  radioChange: function (e) {
    var address_id = e.detail.value;  //选择的地址ID
    if (this.data.from_page == 'checkout') {
      var pages = getCurrentPages();          //获取页面堆栈
      var prevPage = pages[pages.length - 2]; //上一页
      prevPage.setData({
        back_address_id: address_id
      });
    }
    //返回上一页
    wx.navigateBack({
      delta: 1
    });
  },

  //删除收获地址
  deladdress: function (e) {
    var that = this;
    var address_id = e.currentTarget.dataset.addressid;
    wx.showModal({
      title: '提示',
      content: '确定要删除此收货地址吗？',
      success: function (res) {
        if (res.confirm) {
          if (address_id) {
            that.setData({
              del_address_id: address_id
            });
            app.http_req({ Address_ID: address_id, act: 'del_address' }, app.globalData.init.api_url, 'POST', that.delAddress_callback);
          } else {
            wx.showModal({
              title: '错误',
              content: '收货地址id获取失败',
              showCancel: false
            })
          }
        }
      }
    });
  },

  //删除收获地址回调
  delAddress_callback: function (res) {
    if (res.errorCode == 0) {
      //更新收货地址列表
      var addresslist = this.data.addresslist;
      for (var i in addresslist) {
        if (addresslist[i]['Address_ID'] == this.data.del_address_id) {
          addresslist.splice(i, 1);
        }
      }
      //重置删除收货地址id
      this.setData({
        addresslist: addresslist,
        del_address_id: 0
      });
      wx.showToast({
        title: '删除成功',
        icon: 'success',
        duration: 1000
      });
    } else {
      wx.showModal({
        title: '错误',
        content: '删除失败',
        showCancel: false
      });
    }
  },

  //添加收货地址    前台需要使用非冒泡事件:catchtap 因外面有一点击事件 radioChange
  addressAddEdit: function(e){
    //判断是添加还是编辑
    var address_id = e.currentTarget.dataset.addressid;
    wx.navigateTo({
      url: '../addressaddedit/addressaddedit?from=addresslist' + (address_id ? '&addressid=' + address_id : ''),
    })
  },

  //获取收货地址列表
  getAddressList: function (res) {
    if (res.errorCode == 0) {
      var addresslist = res.data;
      this.setData({
        addresslist: addresslist
      });
    } else if (res.errorCode == 1) {
      wx.showModal({
        title: '错误',
        content: res.msg,
        showCancel: false
      })
    }
  },

  //页面加载
  load: function () {
    //获取收货地址列表
    var addressArgs = {
      User_ID: app.globalData.userInfo.User_ID,
      act: 'get_address'
    };
    app.http_req(addressArgs, app.globalData.init.api_url, 'POST', this.getAddressList);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //有addressid 地址选择  来自订单提交->选择收货地址
    if (options.addressid) {
      this.setData({
        check_address_id: options.addressid,
        check_flag: true
      });
    }
    //页面来源
    if (options.from) {
      this.setData({
        from_page: options.from
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //用户登录检查
    if (!app.globalData.userInfo) {
      app.login('', this.load, 'address_list');
      return;
    } else {
      this.load();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  }
})